﻿package little.bunny;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;

public class CustomJiFen extends Activity {
	private Button backButton;
	private Button myButton;
	private TextView textCardNo;
	private String s_card;
	
	private final String serviceNameSpace = "http://littlebunny.com/";
	private final String method_name = "GetCustomCardValue";
	private final String serviceUrl = "http://www.littlebunny.com.cn/bunny_service/bunny_service.asmx";
	private final String SOAP_ACTION = "http://littlebunny.com/GetCustomCardValue";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_jifen);
		
		myButton = (Button) findViewById(R.id.btn_list_title);
		myButton.setText("余额查询");
		
		//SharedPreferences sharedata = getSharedPreferences("user_data", 0);  
		//String s_card = sharedata.getString("card", null);
		
		Bundle bundle = new Bundle();
		bundle = this.getIntent().getExtras();
		s_card = bundle.getString("KEY_CARD");
		
		textCardNo = (TextView) findViewById(R.id.txtCard_ID);
		textCardNo.setText(s_card);
		//************测试数据*******
		//txtInputCard.setText("000120");
		//txtInputPass.setText("15673393");
		//*************************
		showJiFen();
				
		backButton = (Button) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	private void showJiFen() 
	{
    
		if (NetFunction.CheckNetStatus(this)==false)
		{
			Toast.makeText(this, "没有找到有效的网络.", Toast.LENGTH_LONG).show();
			return;
		}
		
		//TextView txtinfo = (TextView) findViewById(R.id.textShowJiFen);

		Toast.makeText(this, "从服务器读取会员信息...", Toast.LENGTH_LONG).show();
		
		final ProgressDialog pDialog = ProgressDialog.show(this, "请稍等",
				"数据加载中...", true);
		
		pDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					pDialog.dismiss();
				}
				return false;
			}
		});
		Thread networkThread = new Thread() {
			@Override
			public void run() {
				SharedPreferences sharedata = getSharedPreferences("user_data", 0);  
				
				String s_pass = sharedata.getString("pass_key", null);
				
				SoapObject request = new SoapObject(serviceNameSpace, method_name);
				request.addProperty("card_id", s_card);
				request.addProperty("u_pass", s_pass);
				
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);
				
				try {
					HttpTransportSE ht = new HttpTransportSE(serviceUrl);
					ht.call(SOAP_ACTION, envelope);

					final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					final String result = response.toString();
					
					// Toast.makeText(this, "读取完毕.", Toast.LENGTH_LONG).show();
					
					pDialog.dismiss();// 隐藏进度条
					
					runOnUiThread(new Runnable() {
						public void run() {
							if (result.startsWith("99:", 0))
							{
								ShowMsg(result.substring(3));
							}
							else
							{
								ShowMsg(result);
							}
						}
					});
				} catch (Exception e) {
					pDialog.dismiss();
					e.printStackTrace();
					TextView txt1;
					txt1 = (TextView) findViewById(R.id.textShowJiFen);
					txt1.setText("网络错误,请检查能否访问网络");
				}
			}
		};

		networkThread.start();
	}
		
	private void ShowMsg(String msg)
	{
		final String info_msg = msg.replace(",", "\n");
				
		TextView result;
		result = (TextView) findViewById(R.id.textShowJiFen);
		result.setText(info_msg);
	}
	
}
